package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.ChapterDAO;
import com.jishaokang.dao.CourseDAO;
import com.jishaokang.dao.TeacherDAO;
import com.jishaokang.model.dto.Course;
import com.jishaokang.model.dto.Learn;
import com.jishaokang.model.dto.User;
import com.jishaokang.model.vo.CourseVO;
import com.jishaokang.model.vo.LearnVO;
import com.jishaokang.service.CourseService;
import com.jishaokang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDAO courseDAO;
    @Autowired
    private TeacherDAO teacherDAO;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private ChapterDAO chapterDAO;

    @Override
    public Result selectCourse(Course course) {
        int total = courseDAO.selectTotal(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.select(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByLearnTotalDesc(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByLabel(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByLearnTotalDesc(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCostAsc(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByCost(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCostAsc(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCostDesc(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByCost(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCostDesc(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCostFree(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByCostFree(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCostFree(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCourseTimeAsc(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByLabel(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCourseTimeAsc(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCourseTimeDesc(Course course) {
        course.setLabelFirst("%"+course.getLabelFirst()+"%");
        course.setLabelSecond("%"+course.getLabelSecond()+"%");
        course.setLabelThird("%"+course.getLabelThird()+"%");
        int total = courseDAO.selectTotalByLabel(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCourseTimeDesc(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByLabelFirst(Course course) {
        int total = courseDAO.selectTotalByLabelFirst(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByLabelFirst(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByLabelSecond(Course course) {
        int total = courseDAO.selectTotalByLabelSecond(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByLabelSecond(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByLabelThird(Course course) {
        int total = courseDAO.selectTotalByLabelThird(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByLabelThird(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByTeacherName(Course course) {
        course.setTeacherName('%'+course.getTeacherName()+'%');
        int total = courseDAO.selectTotalByTeacherNameVague(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByTeacherNameVague(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCourseNameVague(Course course) {
        course.setCourseName("%"+course.getCourseName()+"%");
        int total = courseDAO.selectTotalByCourseNameVague(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByCourseNameVague(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByTeacherNameAndCourseNameVague(Course course) {
        course.setTeacherName('%'+course.getTeacherName()+'%');
        course.setCourseName("%"+course.getCourseName()+"%");
        int total = courseDAO.selectTotalByTeacherNameAndCourseNameVague(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByTeacherNameAndCourseNameVague(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByTeacherNameOrCourseNameVague(Course course) {
        course.setTeacherName('%'+course.getTeacherName()+'%');
        course.setCourseName("%"+course.getCourseName()+"%");
        int total = courseDAO.selectTotalByTeacherNameOrCourseNameVague(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByTeacherNameOrCourseNameVague(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    @Override
    public Result selectCourseByCourseId(Course course) {
        CourseVO courseVO = courseDAO.selectByCourseId(course);
        return ResultCache.getDataOk(courseVO);
    }

    @Override
    public Result addCourse(Course course) {
        int userId= tokenUtil.getUserIdByToken(course.getToken());
        int teacherId = teacherDAO.selectByUserId(userId).getTeacherId();
        course.setTeacherId(teacherId);
        courseDAO.insert(course);
        return ResultCache.OK;
    }

    @Override
    public Result updateCourse(Course course) {
        CourseVO courseVO = courseDAO.selectByCourseId(course);
        if (course.getCourseName() == null) course.setCourseName(courseVO.getCourseName());
        if (course.getCost()==null) course.setCost(courseVO.getCost());
        if (course.getCourseIntroduction() == null) course.setCourseIntroduction(courseVO.getCourseIntroduction());
        if (course.getLabelFirst() == null) course.setLabelFirst(courseVO.getLabelFirst());
        if (course.getLabelSecond()==null) course.setLabelSecond(courseVO.getLabelSecond());
        if (course.getLabelThird() == null) course.setLabelThird(courseVO.getLabelThird());
        courseDAO.update(course);
        return ResultCache.OK;
    }

    @Override
    public Result deleteCourse(Course course) {
        courseDAO.delete(course.getCourseId());
        chapterDAO.deleteByCourseId(course.getCourseId());
        return ResultCache.OK;
    }

    @Override
    public Result selectCourseByUserId(Course course) {
        int userId= tokenUtil.getUserIdByToken(course.getToken());
        int teacherId = teacherDAO.selectByUserId(userId).getTeacherId();
        course.setTeacherId(teacherId);
        int total = courseDAO.selectTotalByTeacherId(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByTeacherId(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

    public static final String KEYSTRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    public static final int len = KEYSTRING.length();

    @Override
    public Result updateCourseImage(String token, Integer courseId,MultipartFile imageFile, HttpServletRequest request) throws IOException {

        Random random = new Random();
        StringBuilder sb = new StringBuilder(new Date().getTime()+"");
        for (int i = 0;i < 50;i++ )
            sb = sb.append(KEYSTRING.charAt(random.nextInt(len)));
        sb.append(".");
        sb.append(imageFile.getOriginalFilename().split("\\.")[1]);
        String path = request.getServletContext().getRealPath("/upload/image/");
        String fileName = sb.toString();
        File targetFile = new File(path, fileName);
        System.out.println(path+"   "+fileName);
        targetFile.createNewFile();
        try {
            imageFile.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseImage("upload/image/"+fileName);
        courseDAO.updateCourseImage(course);
        return ResultCache.OK;
    }


    @Override
    public Result selectCourseByTeacherId(Course course) {
        int total = courseDAO.selectTotalByTeacherId(course);
        course.setPage((course.getPage()-1)*course.getNumber());
        List<CourseVO> courseVOs = courseDAO.selectByTeacherId(course);
        return ResultCache.getListDataOk(total,"courses",courseVOs);
    }

}